package com.bappi.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.util.Log;

public class HttpUtils {

	public static final int HTTP_TIMEOUT = 30 * 1000;

	public static String executeHttpPostAsString(String url,
			ArrayList<NameValuePair> postParameters) throws Exception {
		HttpClient client = getHttpClient();
		HttpPost request = new HttpPost(url);
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
				postParameters);
		request.setEntity(formEntity);
		HttpResponse response = client.execute(request);
		String result = EntityUtils.toString(response.getEntity());
		return result;
	}

	private static HttpClient mHttpClient;

	private static HttpClient getHttpClient() {
		if (mHttpClient == null) {
			mHttpClient = new DefaultHttpClient();
			final HttpParams params = mHttpClient.getParams();
			HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
			HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
			ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
		}
		return mHttpClient;
	}

	public static String executeHttpGetAsString(String url,
			ArrayList<NameValuePair> params) throws Exception {
		String result = null;
		if (params.size() > 0) {
			for (int i = 0; i < params.size(); i++) {
				if (url.indexOf('?') == -1) {
					url = url + "?" + params.get(i).getName() + "="
							+ params.get(i).getValue();
				} else {
					url = url + "&" + params.get(i).getName() + "="
							+ params.get(i).getValue();
				}
			}
		}
		URL u = new URL(url);
		HttpURLConnection c = (HttpURLConnection) u.openConnection();
		c.setConnectTimeout(30000);
		c.setRequestMethod("GET");
		c.connect();
		if (c.getResponseCode() == HttpURLConnection.HTTP_OK) {
			InputStream in = c.getInputStream();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len1 = 0;
			while ((len1 = in.read(buffer)) > 0) {
				baos.write(buffer, 0, len1);
			}
			buffer = baos.toByteArray();
			result = new String(baos.toByteArray());
			baos.close();
			in.close();
			c.disconnect();
		} else {
			c.disconnect();
			throw new Exception("Weak signal please try again!");
		}
		return result;
	}

	public static byte[] getHttpGetByteArrayResponce(String url) throws Exception{
		return getHttpGetByteArrayResponce(url, null);
	}

	public static byte[] getHttpGetByteArrayResponce(String url,
			ArrayList<NameValuePair> params) throws Exception {

		if ((params != null) && (params.size() > 0)) {
			for (int i = 0; i < params.size(); i++) {
				if (url.indexOf('?') == -1) {
					url = url + "?" + params.get(i).getName() + "="
							+ URLEncoder.encode(params.get(i).getValue());
				} else {
					url = url + "&" + params.get(i).getName() + "="
							+ URLEncoder.encode(params.get(i).getValue());
				}
			}
		}
		HttpClient client = getHttpClient();
		HttpGet request = new HttpGet(url);
		HttpResponse response = client.execute(request);
		return EntityUtils.toByteArray(response.getEntity());
	}

	public static HttpURLConnection getHttpURLConnectionm(String url,
			ArrayList<NameValuePair> params) throws Exception {
		HttpURLConnection ret = null;
		if (params.size() > 0) {
			for (int i = 0; i < params.size(); i++) {
				if (url.indexOf('?') == -1) {
					url = url + "?" + params.get(i).getName() + "="
							+ URLEncoder.encode(params.get(i).getValue());
				} else {
					url = url + "&" + params.get(i).getName() + "="
							+ URLEncoder.encode(params.get(i).getValue());
				}
			}
		}
		Log.i("DREG", "URL_BUILD=" + url);
		URL u = new URL(url);
		HttpURLConnection c = (HttpURLConnection) u.openConnection();
		c.setConnectTimeout(30000);
		c.setRequestMethod("GET");
		c.connect();
		if (c.getResponseCode() == HttpURLConnection.HTTP_OK) {
			ret = c;
		} else {
			c.disconnect();
			throw new Exception("Weak signal please try again!");
		}
		return ret;
	}

	public static JSONObject executeHttpPost(String url,
			ArrayList<NameValuePair> postParameters) throws Exception {
		return new JSONObject(executeHttpPostAsString(url, postParameters));
	}

	public static JSONObject login(String url, String userName, String password)
			throws Exception {
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("u", userName));
		postParameters.add(new BasicNameValuePair("p", password));
		return executeHttpPost(url, postParameters);
	}

	// /////////////////////////////////
}
